package com.example.gramediamajalahmobile.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.gramediamajalahmobile.ChannelBean;

public class DBHelperChannel {
	private static final String DB_NAME = "artikel_db";
	private static final int DB_VER = 1;
	public static final String TABLE_NAME = "channelt";
	public static final String COL_ID = "_id";
	public static final String COL_IDC = "channel";
	public static final String COL_NAME = "nama";
	public static final String COL_IMAGES = "images";



	private static final String TAG = "articleDBAdapter";
	private DatabaseHelper dbHelper;
	private SQLiteDatabase db;

	private static final String DB_CREATE = "create table channelt (_id integer primary key ,channel text not null,nama text not null,images integer not null);";

	private final Context context;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			// TODO Auto-generated constructor stub
			super(context, DB_NAME, null, DB_VER);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(DB_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			Log.d(TAG, "upgrade DB");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);

		}

	}

	public DBHelperChannel(Context context) {
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	public DBHelperChannel open() throws SQLException {
		dbHelper = new DatabaseHelper(context);
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	public void createChannel(ChannelBean channel) {
		ContentValues val = new ContentValues();
		val.put(COL_NAME, channel.getNama());
		val.put(COL_IDC,channel.getChannel());
		val.put(COL_IMAGES, channel.getGambar());
		db.insert(TABLE_NAME, null, val);

	
	}

	public boolean deleteChannel(int id) {
		//return db.delete(TABLE_NAME, whereClause, null);
		return db.delete(TABLE_NAME, COL_ID + "=" + id, null) > 0;
	}

	public Cursor getAllChannel() {
		return db.query(TABLE_NAME, new String[] {COL_ID, COL_IDC,COL_NAME ,COL_IMAGES}, null,
				null, null, null, null);
	}

	 public Cursor getSingleChannel(int id)
	 {
	 Cursor cursor = db.query(TABLE_NAME, new String[]
	 {
	 COL_ID,COL_NAME
	 }, COL_ID + "=" + id, null, null, null, null);
	
	 if (cursor != null)
	 cursor.moveToFirst();
	
	 return cursor;
	 }
//
	public boolean updateFavorite(ChannelBean channel) {
		ContentValues val = new ContentValues();
		val.put(COL_NAME, channel.getNama());
		val.put(COL_IDC,channel.getChannel());
		val.put(COL_IMAGES, channel.getGambar());
		
		return db.update(TABLE_NAME, val,
				COL_ID + "=" + channel.getId(), null) > 0;
	}

}

//val.put(COL_NAME, "Pengetahuan");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Komunitas");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Kecantikan");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Fashion");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Teen");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Selingan");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Sepakbola");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Balap");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Otomotif");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Teknologi");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Kesehatan");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Keluarga");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Dongeng");
//db.insert(TABLE_NAME, COL_NAME, val);
//val.put(COL_NAME, "Rumah");
//db.insert(TABLE_NAME, COL_NAME, val);